package com.jiamao.goplay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jiamao.goplay.R;
import com.jiamao.goplay.bean.AnonymousSelectBean;

import org.w3c.dom.Text;

import java.util.List;

public class AnonymouseReportAdapter extends RecyclerView.Adapter<AnonymouseReportAdapter.MyViewHolder> {

    private Context context;

    private List<AnonymousSelectBean> list;

    private onAnonymousItemClick onAnonymousItemClick;

    private int mPosition;

    public AnonymouseReportAdapter(Context context, List<AnonymousSelectBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public AnonymouseReportAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_anonymouse_report_layout, parent, false);
        return new AnonymouseReportAdapter.MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(AnonymouseReportAdapter.MyViewHolder holder, int position) {
        AnonymousSelectBean selectReason = list.get(position);

        holder.tv.setText(selectReason.getReason());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPosition = holder.getAdapterPosition();
                holder.iv.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
               if (onAnonymousItemClick != null) {
                   onAnonymousItemClick.onItemCLick(position, selectReason.getReason());
               }
            }
        });

        if (mPosition == position) {
            holder.iv.setVisibility(View.VISIBLE);
        } else {
            holder.iv.setVisibility(View.GONE);
        }
    }

    public void setOnItemClickListener(onAnonymousItemClick onAnonymousItemClick) {
        this.onAnonymousItemClick = onAnonymousItemClick;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;
        ImageView iv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_report_reason);
            iv = itemView.findViewById(R.id.iv_selected);
        }
    }
}
